<template>
  <div class="module card">
    <div class="module-header">
      <h2><el-icon><OfficeBuilding /></el-icon> 我的班级 - {{ className }}</h2>
    </div>
    <div class="module-body">
      <!-- 选项卡 -->
      <div class="tabs-container">
        <el-button
            v-for="tab in tabs"
            :key="tab.value"
            :type="activeTabClass === tab.value ? 'primary' : 'text'"
            @click="handleTabChange(tab.value)"
            class="tab-button"
        >
          {{ tab.label }}
        </el-button>
      </div>

      <!-- 活动记录 -->
      <div v-if="filteredActivities.length > 0" class="activities-grid">
        <div
            class="activity-item"
            v-for="activity in filteredActivities"
            :key="activity.id"
            @click="handleActivityClick(activity)"
        >
          <div class="activity-icon">
            <el-icon v-if="activity.type === 'notice'"><Bell /></el-icon>
            <el-icon v-else-if="activity.type === 'contact'"><ChatDotRound /></el-icon>
            <el-icon v-else-if="activity.type === 'checkin'"><Clock /></el-icon>
            <el-icon v-else-if="activity.type === 'practice'"><Flag /></el-icon>
            <el-icon v-else><Collection /></el-icon>
          </div>
          <div class="activity-details">
            <div class="activity-title">{{ activity.title }}</div>
            <div class="activity-update">更新: {{ activity.updatedAt }}</div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <el-empty description="暂无活动记录" />
      </div>
    </div>
  </div>

  <!-- 通知详情弹窗 -->
  <el-dialog
      v-model="noticeDetailVisible"
      :title="currentNotice?.title"
      width="60%"
      :before-close="handleCloseNoticeDetail"
  >
    <NoticeDetail
        v-if="currentNotice"
        :notice="currentNotice"
        tab-type="received"
        @close="noticeDetailVisible = false"
    />
  </el-dialog>

  <!-- 活动详情弹窗 -->
  <ActivityDetailDialog
      v-model:visible="activityDetailVisible"
      :activity-id="selectedActivityId"
  />
</template>

<script setup>
import { ref, computed, defineProps, defineEmits } from 'vue';
import { markNoticeAsRead } from '@/api/student';
import { ElMessage } from 'element-plus';
import NoticeDetail from './NoticeDetail.vue';
import ActivityDetailDialog from './ActivityDetailDialog.vue';
import {
  OfficeBuilding, Bell, ChatDotRound,
  Clock, Flag, Collection
} from '@element-plus/icons-vue';

const props = defineProps({
  className: {
    type: String,
    required: true,
    default: '未知班级'
  },
  activities: {
    type: Array,
    required: true,
    default: () => []
  },
  activeTabClass: {
    type: String,
    default: 'all'
  }
});

const emits = defineEmits(['update:activeTabClass']);

// 通知详情相关
const noticeDetailVisible = ref(false);
const currentNotice = ref(null);

// 活动详情相关
const activityDetailVisible = ref(false);
const selectedActivityId = ref(null);

// 选项卡定义
const tabs = ref([
  { label: '全部', value: 'all' },
  { label: '通知', value: 'notice' },
  { label: '联系', value: 'contact' },
  { label: '打卡', value: 'checkin' },
  { label: '实践', value: 'practice' }
]);

// 根据当前选项卡过滤活动
const filteredActivities = computed(() => {
  if (props.activeTabClass === 'all') {
    return props.activities;
  }
  return props.activities.filter(
      activity => activity.type === props.activeTabClass
  );
});

// 处理选项卡变化
const handleTabChange = (value) => {
  emits('update:activeTabClass', value);
};

// 处理活动点击
const handleActivityClick = async (activity) => {
  console.log('点击活动:', activity);

  // 如果是通知类型，显示通知详情弹窗
  if (activity.type === 'notice') {
    await handleNoticeClick(activity);
  } else {
    // 其他类型的活动（联系、打卡、实践）显示活动详情弹窗
    handleOtherActivityClick(activity);
  }
};

// 处理通知点击 - 显示通知详情弹窗
const handleNoticeClick = async (notice) => {
  console.log('打开通知:', notice);

  try {
    const noticeId = notice.id;

    if (!noticeId) {
      ElMessage.error('无法获取通知ID');
      return;
    }

    // 获取用户信息
    const userInfoStr = localStorage.getItem('userInfo');
    const userInfo = userInfoStr ? JSON.parse(userInfoStr) : null;
    const userId = userInfo?.id;

    if (!userId) {
      ElMessage.error('无法获取用户信息，请重新登录');
      return;
    }

    // 使用完整通知数据
    currentNotice.value = {
      noticeId: notice.id,
      title: notice.title,
      content: notice.content,
      typeName: notice.noticeTypeName,
      senderName: notice.senderName,
      createTime: notice.createTime || notice.updatedAt,
      description: notice.description
    };

    noticeDetailVisible.value = true;

    // 标记为已读
    await markNoticeAsRead(parseInt(noticeId), parseInt(userId));

  } catch (error) {
    console.error('打开通知详情失败:', error);
    ElMessage.error('打开通知详情失败');
  }
};

// 处理其他类型活动点击 - 显示活动详情弹窗
const handleOtherActivityClick = (activity) => {
  console.log('打开活动:', activity);

  // 设置活动ID并显示活动详情弹窗
  if (activity.id) {
    selectedActivityId.value = activity.id;
    activityDetailVisible.value = true;
  } else {
    ElMessage.info('该活动暂无详细信息');
  }
};

// 关闭通知详情弹窗
const handleCloseNoticeDetail = () => {
  noticeDetailVisible.value = false;
  currentNotice.value = null;
};
</script>

<style scoped>
.module {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  min-height: 200px;
  display: flex;
  flex-direction: column;
}

.module:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.module-header {
  position: relative;
  padding: 16px 20px;
  background: #1e62ec;
  color: white;
  display: flex;
  align-items: center;
}

.module-header h2 {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.module-body {
  padding: 20px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

/* 选项卡容器 */
.tabs-container {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.tab-button {
  padding: 8px 15px;
  border-radius: 16px;
  transition: all 0.3s;
}

.tab-button:not(.el-button--primary) {
  background: #f5f7fa;
  color: #606266;
}

.tab-button:hover:not(.el-button--primary) {
  background: #e8f4ff;
  color: #1e62ec;
}

/* 活动网格布局 */
.activities-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.activity-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-radius: 8px;
  background: #f9fafc;
  transition: all 0.3s;
  cursor: pointer;
  height: 100%;
  box-sizing: border-box;
  border: 1px solid #ebeef5;
}

.activity-item:hover {
  background: #e8f4ff;
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}

.activity-icon {
  width: 50px;
  height: 50px;
  background: #e6f1ff;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  flex-shrink: 0;
}

.activity-icon .el-icon {
  font-size: 28px;
  color: #1e62ec;
}

.activity-details {
  flex-grow: 1;
  min-width: 0;
}

.activity-title {
  font-size: 15px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.activity-update {
  font-size: 13px;
  color: #909399;
}

/* 空状态样式 */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #a8a8a8;
  font-style: italic;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .activities-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .activity-item {
    padding: 12px;
  }

  .activity-icon {
    width: 44px;
    height: 44px;
    margin-right: 12px;
  }

  .activity-icon .el-icon {
    font-size: 24px;
  }

  .activity-title {
    font-size: 14px;
  }

  .activity-update {
    font-size: 12px;
  }

  .tab-button {
    padding: 6px 12px;
    font-size: 13px;
  }
}

/* 通知详情弹窗样式 */
:deep(.el-dialog__header) {
  background: #1e62ec;
  color: white;
  margin: 0;
  padding: 16px 20px;
}

:deep(.el-dialog__title) {
  color: white;
  font-size: 18px;
  font-weight: 600;
}

:deep(.el-dialog__headerbtn) {
  top: 16px;
}

:deep(.el-dialog__headerbtn .el-dialog__close) {
  color: white;
}
</style>
